package com.example.fantacylib

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.databinding.DataBindingUtil
import com.example.baseutils.Constance
import com.example.baseutils.MySharedPreferences
import com.example.baseutils.activity.BaseLoadActivity
import com.example.baseutils.read
import com.example.baseutils.showToast
import com.example.baseutils.start
import com.example.fantacylib.databinding.ActivityWaitBinding
import com.example.printlib.PrintEngine
import com.example.printlib.PrintService

/**
 * 付款后等待打印的页面
 * author：lp
 * 2024年09月04日16:30:50
 * */
class WaitActivity : BaseLoadActivity() {
    private lateinit var binding: ActivityWaitBinding
    private var printState = false
    private val mAutoOutTime = 10
    private var autoOutTimer: CountDownTimer? = null
    override fun onLoad() {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_wait)
        binding.btnBack.setOnClickListener {
            if (printState)
                PayActivity().start(this@WaitActivity)
            else {
                AlertDialog.Builder(this@WaitActivity)
                    .setTitle("注意！")
                    .setMessage("还未打印成功，是否退出页面?！")
                    .setNegativeButton("等待打印") { dialog, which ->
                        //重复超时计时
                        dialog.dismiss()
                    }
                    .setPositiveButton("回到主页") { dialog, which ->
                        dialog.dismiss()
                        FantacyMain().start(this@WaitActivity)
                        finish()
                    }
                    .show()
            }
        }
        PrintEngine.getInstance().addPrintCallback(printCallback)
        PrintEngine.getInstance().addPrintQueue(CaptureActivity.localPath)
        binding.tvPhone.text= "服务电话: "+MySharedPreferences.read(Constance.SERVICE_CALL,"000")
    }

    private val printCallback : PrintService.PrintCallback=
        PrintService.PrintCallback { state, description ->
            if (binding.tvPrint.visibility == View.GONE && (state == PrintService.PrintServerStatus.disconnected || state == PrintService.PrintServerStatus.error)) {
                "连接打印机失败".showToast(this@WaitActivity)
                binding.tvPrint.visibility = View.VISIBLE
                return@PrintCallback
            }
            if (binding.tvPrint.visibility == View.VISIBLE && (state != PrintService.PrintServerStatus.disconnected)) {
                "连接成功".showToast(this@WaitActivity)
                binding.tvPrint.visibility = View.GONE
            }
            if(!printState&&state==PrintService.PrintServerStatus.printOk){
                printState=true
                binding.tvNotify.text="打印成功，请取走照片"
                autoOutTimer()
            }
        }
    override fun onStart() {
        super.onStart()
        if(printState)
            autoOutTimer()
    }

    override fun onStop() {
        super.onStop()
        autoOutTimer?.cancel()
    }

    private fun autoOutTimer(force: Boolean = false) {
        autoOutTimer?.cancel()
        autoOutTimer = object : CountDownTimer(mAutoOutTime * 1000L, 1000L) {
            override fun onTick(p0: Long) {}
            override fun onFinish() {
                if (force) {
                    FantacyMain().start(this@WaitActivity)
                    return
                }
                AlertDialog.Builder(this@WaitActivity)
                    .setTitle("打印成功")
                    .setMessage("即将在 $mAutoOutTime 秒后自动退出到主页面！")
                    .setNegativeButton("稍等一会儿") { dialog, which ->
                        //重复超时计时
                        autoOutTimer()
                    }
                    .setPositiveButton("退出吧") { dialog, which ->
                        dialog.dismiss()
                        finish() }.show()
                autoOutTimer(true)
            }
        }.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        PrintEngine.getInstance().removePrintCallback(printCallback)
    }
}